#pragma once

#include <iostream>
#include <string>
#include <functional>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

class InetAddr
{
public:
    InetAddr(sockaddr_in addr)
        :_addr(addr)
    {
        ToHost(addr);
    }

    std::string IP()
    {
        return _ip;
    }
    uint16_t Port()
    {
        return _port;
    }

    std::string Info()
    {
        return "[" + std::to_string(_port) + "]:" + _ip;
    }

private:
    void ToHost(sockaddr_in addr)
    {
        _port = ntohs(addr.sin_port);
        char ip_buff[32];
        inet_ntop(AF_INET, &addr.sin_addr, ip_buff, sizeof(ip_buff));
        _ip = ip_buff;
    }    


private:
    std::string _ip;
    uint16_t _port;
    sockaddr_in _addr;
};